package com.example.myzhugezhaofang.fragment.consultingfrag;

import android.content.Intent;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mvplibrary.ui.fragment.BaseMvpFragment;
import com.example.myzhugezhaofang.R;
import com.example.myzhugezhaofang.activity.WebActivity;
import com.example.myzhugezhaofang.adapter.ConsultingAdapter;
import com.example.myzhugezhaofang.bean.ConsultingBean;
import com.example.myzhugezhaofang.callback.Mainconcations;
import com.example.myzhugezhaofang.model.MainModel;
import com.example.myzhugezhaofang.presenter.MainPresenter;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * wikipediaMap.put("modular", "zgbk");
 * 作者：朱勇闯
 * data : 2021/4/16  11:42
 * 作用： 资讯下市场百科
 */
public class WikipediaFragment<T> extends BaseMvpFragment<Mainconcations.MainView, MainModel, MainPresenter<T>> implements Mainconcations.MainView<T> {
    @BindView(R.id.rv_consulting_item)
    RecyclerView rv_list;
    @BindView(R.id.smart_consulting_item)
    SmartRefreshLayout smart;
    private static final String TAG = "朱勇闯Recommended";
    private List<ConsultingBean.ListBean> list_con;
    private HashMap<String, Object> map;
    private ConsultingAdapter consultingAdapter;
    private int page = 1;

    @Override
    protected MainPresenter<T> createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected void initEvent() {
        list_con = new ArrayList<>();
        rv_list.setLayoutManager(new LinearLayoutManager(context));
        consultingAdapter = new ConsultingAdapter(list_con, context, R.layout.item_consulting);

        rv_list.setAdapter(consultingAdapter);
        consultingAdapter.setItemListener(new ConsultingAdapter.ItemListener() {
            @Override
            public void onItemClickListener(int pos) {
                String m_url = list_con.get(pos).getM_url();
                Intent intent = new Intent(context, WebActivity.class);
                intent.putExtra("link", m_url);
                startActivity(intent);
            }
        });
        map = new HashMap<>();
        map.put("pageLimit", "10");
        map.put("pageStart", "1");
        map.put("modular", "zgbk");
//        map.put("operaVersion", "5.1.1");
//        map.put("s_screen_width", "1080");
//        map.put("token", "i8la_XdUQEEakWXuDmGDpc_1k4f37-9C0-9e6R9UGo2f4b4UILcmwjKoIX1ih-AMOub_rNjArGlBogh-9CUtODg-2Ge4Qt-72kSZOn-Dt7bndh7yqazHVtiGxUOPU1s1");
        map.put("city", "bj");
//        map.put("app_name", "zgzf");
//        map.put("s_manufacturer", "OnePlus");
//        map.put("s_carrier", "CHINA MOBILE");
//        map.put("appVersion", "4.5.9.0");
//        map.put("imei", "865166029216929");
//        map.put("s_ip", "172.16.2.10");
//        map.put("s_wifi", "true");
        map.put("platform_type", "4");
//        map.put("s_app_version", "4.5.9.0");
//        map.put("s_os", "Android");
//        map.put("appName", "zgzf");
//        map.put("user_token", "i8la_XdUQEEakWXuDmGDpc_1k4f37-9C0-9e6R9UGo2f4b4UILcmwjKoIX1ih-AMOub_rNjArGlBogh-9CUtODg-2Ge4Qt-72kSZOn-Dt7bndh7yqazHVtiGxUOPU1s1");
        map.put("platformType", "4");
//        map.put("user_type", "C端用户");
//        map.put("ip", "172.16.2.10");
//        map.put("device_only_id", "00000000-788c-2f4e-0000-0000109192c6");
//        map.put("appSignatures", "893daee49856e6e0c4661f53919198c3");
//        map.put("s_os_version", "5.1.1");
//        map.put("device_from", "4");
//        map.put("s_screen_height", "1920");
//        map.put("s_model", "HD1910");
        mPresenter.getData(map, "https://api.zhuge.com/", "contentService/v1/News/appNewsList", 0);
        smart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                map.put("pageStart", page);
                mPresenter.getData(map, "https://api.zhuge.com/", "contentService/v1/News/appNewsList", 0);
                smart.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                list_con.clear();
                map.put("pageStart", page);
                mPresenter.getData(map, "https://api.zhuge.com/", "contentService/v1/News/appNewsList", 0);
                consultingAdapter.notifyDataSetChanged();
                smart.finishRefresh();
            }
        });

    }

    @Override
    protected int createLayoutId() {
        return R.layout.fragment_consulting_item;
    }

    @Override
    public void showList(Object t, int type) {
        if (type == 0) {
            ConsultingBean newsRecommendBean = new Gson().fromJson((JsonElement) t, ConsultingBean.class);
            List<ConsultingBean.ListBean> list = newsRecommendBean.getList();
            for (int i = 0; i < list.size(); i++) {
                list_con.add(list.get(i));
            }
//            recommendlist.addAll(list);
            consultingAdapter.notifyDataSetChanged();
            Log.e(TAG, "showList数据打印成功11111: " + list.get(0).getTitle());
            Log.e(TAG, "showList数据打印成功11111: " + list.get(0).getThumb());
        }
    }

    @Override
    public void showError(String error) {

    }

    @Override
    public void showProgressbar() {

    }

    @Override
    public void hideProgressbar() {

    }

    private void initFinish() {
//        smart.finishRefresh();
//        smart.finishLoadMore();
    }
}

